package com.cn.yang.dao.impl;

import com.cn.yang.dao.AboutUsDao;
import com.cn.yang.model.aboutUs.AboutUsModel;
import com.cn.yang.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AboutUsDaoImpl implements AboutUsDao {
    JDBC jdbc = new JDBC();
    //查询 AboutUs
    @Override
    public AboutUsModel findAboutUs(AboutUsModel model) {
        String sql = "select * from aboutus ";
        ResultSet resultSet = jdbc.getResultSet(sql);
        AboutUsModel message = new AboutUsModel();
        try {
            while (resultSet.next()) {
                message.setId(resultSet.getInt("id"));
                message.setTitle(resultSet.getString("title"));
                message.setBgimg(resultSet.getString("bgimg"));
                message.setContent(resultSet.getString("content"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return message;
    }
    public AboutUsModel findAboutUs() {
        String sql = "select * from aboutus ";
        ResultSet resultSet = jdbc.getResultSet(sql);
        AboutUsModel message = new AboutUsModel();
        try {
            while (resultSet.next()) {
                message.setId(resultSet.getInt("id"));
                message.setTitle(resultSet.getString("title"));
                message.setBgimg(resultSet.getString("bgimg"));
                message.setContent(resultSet.getString("content"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return message;
    }
// 返回总条数
    @Override
    public int countAboutUs() {
        String sql = "select count(*) as count  from aboutus ";
        ResultSet result = jdbc.getResultSet(sql);
        int count = 0;
        try{
            while (result.next()){
                count = result.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
//查询 通过ID
    @Override
    public AboutUsModel selectById(Integer id) {
        String sql = "select * from aboutus where id = "+id;
        ResultSet resultSet = jdbc.getResultSet(sql);
        AboutUsModel model = new AboutUsModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setBgimg(resultSet.getString("bgimg"));
                model.setContent(resultSet.getString("content"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return model;
    }
//更新 AboutUs
    @Override
    public int updateAboutUs(AboutUsModel model) {
        String sql = "update aboutus set title = '"+model.getTitle()+"'," +
                "bgimg ='"+model.getBgimg()+"'," +
                "content ='"+model.getContent()+"'";
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
}
